package com.lxt.cloud.service.impl;

import com.lxt.cloud.entity.MyUserDetails;
import com.lxt.cloud.entity.User;
import com.lxt.cloud.feign.UserServiceFeign;
import com.lxt.cloud.service.MyUserDetailsService;
import com.lxt.common.mvc.msg.output.ApiResult;
import com.lxt.common.vo.UserModel;
import org.springframework.context.annotation.Primary;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

@Primary
@Service("userService")
public class MyUserDetailsServiceImpl implements MyUserDetailsService {
    @Resource
    UserServiceFeign userServiceFeign;

    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
        ApiResult<UserModel> auser= userServiceFeign.checkLogin(username);
        if(null!=auser.getData()){
            MyUserDetails myUserDetails = new MyUserDetails(auser.getData());
            return myUserDetails;
        }else{
            throw new UsernameNotFoundException("there's no user founded!");
        }
    }
}